﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using wUniApp.MVC.App_Data;

namespace wUniApp.MVC.Controllers
{
  public class InheritanceProceduresController : wBaseController<InheritanceProcedure>
  {
    // GET: InheritanceProcedures
    public override ActionResult Index()
    {
      var inheritanceProcedures = dbc.InheritanceProcedures.Include(i => i.Customer);
      return View(inheritanceProcedures.ToList());
    }

    [HttpGet]
    //public JsonResult IndexJS()
    public ActionResult IndexJS()
    {
      var inheritanceProcedures = dbc.InheritanceProcedures.Include(i => i.Customer);
      var res = new List<object>();
      foreach (var item in inheritanceProcedures)
      {
        res.Add(new { name = item.Customer.FirstName + " " + item.Customer.LastName, description = item.Description, amount = item.AmountDue });
      }
      return Json(res, JsonRequestBehavior.AllowGet);
    }

    // GET: InheritanceProcedures/Create
    public override ActionResult Create()
    {
      ViewBag.CustomerId = new SelectList(dbc.Customers, "Id", "FirstName");
      return View();
    }

    // POST: InheritanceProcedures/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public override ActionResult Create([Bind(Include = "Id,Description,CustomerId,AmountDue")] InheritanceProcedure inheritanceProcedure)
    {
      if (ModelState.IsValid)
      {
        dbc.InheritanceProcedures.Add(inheritanceProcedure);
        dbc.SaveChanges();
        return RedirectToAction("Index");
      }

      ViewBag.CustomerId = new SelectList(dbc.Customers, "Id", "FirstName", inheritanceProcedure.CustomerId);
      return View(inheritanceProcedure);
    }

    // GET: InheritanceProcedures/Edit/5
    public override ActionResult Edit(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      InheritanceProcedure inheritanceProcedure = dbc.InheritanceProcedures.Find(id);
      if (inheritanceProcedure == null)
      {
        return HttpNotFound();
      }
      ViewBag.CustomerId = new SelectList(dbc.Customers, "Id", "FirstName", inheritanceProcedure.CustomerId);
      return View(inheritanceProcedure);
    }

    // POST: InheritanceProcedures/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public override ActionResult Edit([Bind(Include = "Id,Description,CustomerId,AmountDue")] InheritanceProcedure inheritanceProcedure)
    {
      if (ModelState.IsValid)
      {
        dbc.Entry(inheritanceProcedure).State = EntityState.Modified;
        dbc.SaveChanges();
        return RedirectToAction("Index");
      }
      ViewBag.CustomerId = new SelectList(dbc.Customers, "Id", "FirstName", inheritanceProcedure.CustomerId);
      return View(inheritanceProcedure);
    }
  }
}
